package com.yunao.commons.metasearch.ms;

import com.yunao.commons.enums.SearchTypeEnum;
import com.yunao.commons.form.MtForm;
import com.yunao.commons.metasearch.common.Convert;
import com.yunao.commons.metasearch.entity.CatchResult;
import com.yunao.commons.metasearch.entity.CatchResults;
import org.apache.commons.codec.binary.Base64;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class MsSystem {

	public static void main(String[] args) throws Exception {
		MsSystem ms = MsSystem.getInstance();

		MtForm mt = new MtForm();
		mt.setWord("圣诞");
//		mt.setSort("t");
//		mt.setRange("d");
//		mt.setDup("fq");
		mt.setType(SearchTypeEnum.MS_WEIBO.getSearchType());
//		mt.setSource(SearchTypeEnum.MS_WEBPAGE_BING.getSearchType());

		CatchResults rsts = ms.query(mt);
		for (CatchResult rst : rsts) {
			System.out.println(rst);
		}
	}

	private static MsSystem instance = null;

	private MsSystem() {
		super();
	}

	public static MsSystem getInstance() throws Exception {
		if (instance == null) {
			instance = new MsSystem();
//			instance.load("parse/ms.xml");
			instance.load();
		}
		return instance;
	}

	/**
	 * 抓取 Cookie，加载配置文件
	 */
	public static void init() throws Exception {
		// 加载 ms.xml 配置文件
		MsSystem instance = MsSystem.getInstance();
//		instance.load("parse/ms.xml");
		instance.load();
	}

	/**
	 * 元搜索检索
	 * 
	 * @param mt
	 *            检索条件
	 */
	public CatchResults query(MtForm mt) throws Exception {
		String word = mt.getWord();

		if (word == null || word.length() == 0)
			return null;
		String type = mt.getType();
		String source = mt.getSource();
		if (source == null || source.length() == 0 || source.equals("all"))
			source = getNodeAll(type);
		String[] names = Convert.split(source, ",");
		if (names == null || names.length == 0)
			return null;
		CatchResults rsts = null;
		NumberFormat format = NumberFormat.getInstance();
		// if (mt.getType().equals("weibo")) {
		// loadAllCookies();
		// }

		if (names.length == 1) {
			// 只有一个来源时使用单线程
			rsts = new MsThread(mt, source).call();
			//System.out.println(rsts.size());
			// System.out.println("testing:"+source);

			// rsts.getMap().put(names[0], format.format(rsts.getTotal()));
		} else {
			Map<String, String> map = new HashMap<String, String>();
			// 线程池
			ExecutorService pool = Executors.newFixedThreadPool(names.length);
			List<Future<CatchResults>> futures = new ArrayList<Future<CatchResults>>();
			for (String name : names) {
				Future<CatchResults> future = pool.submit(new MsThread(mt, name));
				futures.add(future);
				try {
					map.put(name, format.format(future.get().getTotal()));
				} catch (Exception e) {

				}
			}
			// 关闭线程池
			pool.shutdown();
			rsts = new CatchResults();
			for (Future<CatchResults> future : futures) {
				CatchResults fs = future.get();
				if (fs != null) {
					rsts.setTotal(rsts.getTotal() + fs.getTotal());
					rsts.addAll(fs);
				}
			}
			rsts.setMap(map);
		}
		return rsts;
	}

	/**
	 * 元搜索加载配置文件
	 *
	 *            配置文件名称，文件存放于 conf 文件夹，
	 */
//	public void load(String fileName) throws Exception {
	public void load() throws Exception {
		org.w3c.dom.Document doc = null;
		try {

			String xmlstr  = "<root>\n" +
					"\t<news>\n" +
					"\t\t<source>baidu,qihu,sogou,chinaso</source>\n" +
					"\t\t<!-- 中国搜索新闻 -->\n" +
					"\t\t<chinaso url=\"http://news.chinaso.com/newssearch.htm?q=%word%\" page=\"page=%page%\" \n" +
					"            range=\"startTime=24h&amp;endTime=now,startTime=1w&amp;endTime=now,startTime=1m&amp;endTime=now\" sort=\"order=,order=time\" encode=\"utf8\">\n" +
					"            <result value=\"div[class=toolTab_xgxwts]\"></result>\n" +
					"            <all value=\"div[class=resultWrapper] ol[class=seResult]\"></all>\n" +
					"            <items value=\"li[class=reItem]\"></items>\n" +
					"            <title value=\"h2 a\"></title>\n" +
					"            <href value=\"h2 a(href)\"></href>\n" +
					"            <img value=\"div[class=reNewsImgWrapper] img(src)\"></img>\n" +
					"            <summary value=\"div[class=reNewsWrapper] p!p[class=snapshot]\"></summary>\n" +
					"            <info value=\"div[class=reNewsWrapper] p[class=snapshot] span\"></info>\n" +
					"            <more value=\"div[class=reNewsWrapper] p[class=snapshot] a\"></more>\n" +
					"            <links value=\"div[class=reNewsWrapper] p[class=snapshot] a(href)\"></links>\n" +
					"\t\t</chinaso>\n" +
					"\t\t<!-- 搜狗新闻 -->\n" +
					"\t\t<sogou url=\"https://news.sogou.com/news?query=%word%\" page=\"page=%page%\" sort=\"sort=0,sort=1\"\n" +
					"\t\t\trange=\"tsn=1,tsn=2,tsn=3\"   encode=\"utf8\">\n" +
					"\t\t\t<result value=\"span[class=filt-result]\"></result>\n" +
					"\t\t\t<all value=\"div[id=main]\"></all>\n" +
					"\t\t\t<items value=\"div[class=vrwrap]\"></items>\n" +
					"\t\t\t<title value=\"h3[class=vrTitle] a\"></title>\n" +
					"\t\t\t<href value=\"h3[class=vrTitle] a(href)\"></href>\n" +
					"\t\t\t<img value=\"a[class=news-pic] img(src)\"></img>\n" +
					"\t\t\t<summary value=\"p[class=news-txt] span\"></summary>\n" +
					"\t\t\t<info value=\"p[class=news-from]\"></info>\n" +
					"            <more value=\"a[id=news_similar]\"></more>\n" +
					"            <links value=\"a[id=news_similar](href)\"></links>\n" +
					"\t\t</sogou>\n" +
					"\t\t<!-- 百度新闻 -->\n" +
					"\t\t<baidu url=\"http://www.baidu.com/s?word=%word%&amp;cl=2&amp;tn=news&amp;ie=utf-8&amp;bt=0&amp;et=0\" page=\"pn=(%page%-1)*10\"\n" +
					"\t\t\trange=\"lm=1,lm=7,lm=30\" sort=\"rtt=1,rtt=4\" encode=\"utf8\">\n" +
					"\t\t\t<result value=\"span[class=nums]\"></result>\n" +
					"\t\t\t<all value=\"div[id=content_left] div(3)\"></all>\n" +
					"\t\t\t<items value=\"div[class=result]\"></items>\n" +
					"\t\t\t<title value=\"h3[class=c-title] a\"></title>\n" +
					"\t\t\t<href value=\"h3[class=c-title] a(href)\"></href>\n" +
					"\t\t\t<img value=\"a[class=c_photo] img(src)\"></img>\n" +
					"\t\t\t<info value=\"p[class=c-author]\"></info>\n" +
					"\t\t\t<summary value=\"div[class=c-summary]!p[class=c-author]!span[class=c-info]\"></summary>\n" +
					"\t\t\t<more value=\"a[class=c-more_link]\"></more>\n" +
					"\t\t\t<links value=\"a[class=c-cache](href)\"></links>\n" +
					"\t\t</baidu>\n" +
					"\t\t\t<!-- 360新闻 -->\n" +
					"\t\t<qihu url=\"http://news.so.com/ns?q=%word%&amp;tn=news&amp;j=0\" page=\"pn=%page%\"\n" +
					"\t\t\tsort=\"rank=rank,rank=pdate\" \t\t\trange=\"sd=2,sd=3,sd=4\" encode=\"gb2312\">\n" +

					"\t\t\t<result value=\"div[class=filter-total]\"></result>\n" +
					"\t\t\t<all value=\"ul[class=result]\"></all>\n" +
					"\t\t\t<items value=\"li[class=full-txt]\"></items>\n" +
					"\t\t\t<title value=\"h3[class=js-title] a(title)\"></title>\n" +
					"\t\t\t<href value=\"h3[class=js-title] a(href)\"></href>\n" +
					"\t\t\t<img value=\"div[class=img-box] a img(src)\"></img>\n" +
					"\t\t\t<info value=\"div[class=info]\"></info>\n" +
					"\t\t\t<summary value=\"div[class=summary]\"></summary>\n" +
					"\t\t\t<more value=\"div[class=info] span(1)\"></more>\n" +
					"\t\t\t<links value=\"p[class=newsinfo] a(href)\"></links>\n" +
					"\t\t</qihu>\n" +
					"\t</news>\n" +
					"\t<webpage>\n" +
					"\t\t<source>baidu,sogou,qihu,bing,chinaso</source>\n" +
					"\t\t<!-- 中国搜索 -->\n" +
					"\t\t<chinaso url=\"http://www.chinaso.com/search/pagesearch.htm?q=%word%\" page=\"page=%page%\" \n" +
					"            encode=\"utf8\">\n" +
					"            <result value=\"span[class=pageTotal]\"></result>\n" +
					"            <all value=\"div[class=resultWrapper] ol[class=seResult]\"></all>\n" +
					"            <items value=\"li[class=reItem]\"></items>\n" +
					"            <info value=\"p[class=snapshot] span\"></info>\n" +
					"            <title value=\"h2 a\"></title>\n" +
					"            <href value=\"h2 a(href)\"></href>\n" +
					"            <summary value=\"div[class=reNewsWrapper] p!p[class=snapshot]\"></summary>\n" +
					"            <links value=\"div[class=reNewsWrapper] p[class=snapshot]\"></links>\n" +
					"\t\t</chinaso>\n" +
					"\t\t<!-- 必应 -->\n" +
					"\t\t<bing url=\"https://cn.bing.com/search?q=%word%&amp;FORM=PERE\" page=\"first=(%page%-1)*10\"\n" +
					"\t\t\trange=\"filters=ex1%3a&quot;ez1&quot;,filters=ex1%3a&quot;ez2&quot;,filters=ex1%3a&quot;ez3&quot;\" encode=\"utf8\">\n" +
					"            <result value=\"div[id=b_tween] span[class=sb_count]\"></result>\n" +
					"            <all value=\"ol[id=b_results]\"></all>\n" +
					"            <items value=\"li[class=b_algo]\"></items>\n" +
					"            <title value=\"h2 a\"></title>\n" +
					"            <pubdate value=\"div[class=b_caption] p!p\"></pubdate>\n" +
					"            <href value=\"h2 a(href)\"></href>\n" +
					"            <summary value=\"div[class=b_caption] p\"></summary>\n" +
					"            <links value=\"div[class=b_caption] cite\"></links>\n" +
					"\t\t</bing>\n" +
					"\t\t<!-- 百度网页 -->\n" +
					"\t\t<baidu url=\"http://www.baidu.com/s?wd=%word%&amp;ie=utf-8\" page=\"pn=(%page%-1)*10\"\n" +
					"\t\t\trange=\"lm=1,lm=7,lm=30\"  encode=\"utf8\">\n" +
					"\t\t\t<result value=\"div[class=nums] span[class=nums_text]\"></result>\n" +
					"\t\t\t<all value=\"div[id=content_left]\"></all>\n" +
					"\t\t\t<items value=\"div[class=c-container]\"></items>\n" +
					"\t\t\t<pubdate value=\"div[class=c-abstract] span\"></pubdate>\n" +
					"\t\t\t<info value=\"div[class=f13] span\"></info>\n" +
					"\t\t\t<title value=\"h3[class=t] a\"></title>\n" +
					"\t\t\t<href value=\"h3[class=t] a(href)\"></href>\n" +
					"\t\t\t<summary value=\"div[class=c-abstract]!div\"></summary>\n" +
					"\t\t\t<links value=\"a[class=c-showurl]\"></links>\n" +
					"\t\t</baidu>\n" +
					"\t\t<!-- 搜搜网页 -->\n" +
					"\t\t<sogou url=\"https://www.sogou.com/web?query=%word%&amp;num=10\" page=\"page=%page%\"\n" +
					"\t\t\trange=\"tsn=1,tsn=2,tsn=3\"  encode=\"utf8\">\n" +
					"\t\t\t<result value=\"div[id=main] p[class=num-tips]\"></result>\n" +
					"\t\t\t<all value=\"div[class=results]\"></all>\n" +
					"\t\t\t<items value=\"div[class=vrwrap]\"></items>\n" +
					"\t\t\t<title value=\"h3[class=vrTitle] a\"></title>\n" +
					"\t\t\t<pubdate value=\"div[class=fb] cite\"></pubdate>\n" +
					"\t\t\t<info value=\"div[class=fb] cite\"></info>\n" +
					"\t\t\t<title value=\"h3[class=vrTitle] a\"></title>\n" +
					"\t\t\t<href value=\"h3[class=vrTitle] a(href)\"></href>\n" +
					"\t\t\t<summary value=\"p[class=str_info]\"></summary>\n" +
					"\t\t\t<links value=\"div[class=fb] cite\"></links>\n" +
					"\t\t</sogou>\n" +
					"\t\t\t<!-- 360网页 -->\n" +
					"\t\t<qihu url=\"http://www.so.com/s?q=%word%&amp;j=0&amp;ls=0&amp;src=srp_paging&amp;fr=bd_youtu29\" page=\"pn=%page%\"\n" +
					"\t\t\trange=\"adv_t=d,adv_t=w,adv_t=m\"  encode=\"gb2312\">\n" +
					"\t\t\t<result value=\"span[class=nums]\"></result>\n" +
					"\t\t\t<all value=\"ul[class=result]\"></all>\n" +
					"\t\t\t<items value=\"li\"></items>\n" +
					"\t\t\t<info value=\"span[class=tip-v]\"></info>\n" +
					"\t\t\t<title value=\"h3[class=res-title] a\"></title>\n" +
					"\t\t\t<href value=\"h3[class=res-title] a(href)\"></href>\n" +
					"\t\t\t<info value=\"p[class=res-linkinfo] cite\"></info>\n" +
					"\t\t\t<pubdate value=\"span[class=lst-time]\"></pubdate>\n" +
					"\t\t\t<summary value=\"p[class=res-desc]!a\"></summary>\n" +
					"\t\t\t<links value=\"p[class=res-linkinfo] cite\"></links>\n" +
					"\t\t</qihu>\n" +
					"\t</webpage>\n" +
					"\t<weibo>\n" +
					"\t\t<source>sina</source>\n" +
					"\t\t<!-- 新浪微博 -->\n" +
					"\t\t<sina url=\"https://s.weibo.com/weibo?q=%word% &amp;suball=1&amp;timescope=custom:%begin%:%end%&amp;Refer=g\"\n" +
					"\t\t\t  encode=\"utf8\" sort=\"xsort=hot,xsort=time\">\n" +
					"\t\t\t<!-- ip=\"202.85.215.177\" port=\"7008\" set=\"true\" type=\"4\" > -->\n" +
					"\t        <info value=\"http://s.weibo.com/weibo/%word%\"></info>\n" +
					"\t\t\t<result value=\"div[class=search_num] span\"></result>\n" +
					"\t\t\t<all value=\"div[class=m-wrap]\"></all>\n" +
					"\t\t\t<items value=\"div[class=card-wrap]\"></items>\n" +
					"\n" +
					"\t\t\t<reserved value=\"div[class=con] div\">\n" +
					"\t\t\t\t<summary value=\"p[class=txt]\"></summary>\n" +
					"\t\t\t\t<pubdate value=\"p[class=from] a\"></pubdate>\n" +
					"\t\t\t\t<href value=\"p[class=from] a(href)\"></href>\n" +
					"\t\t\t\t<source value=\"p[class=from] a[rel=nofollow]\"></source>\n" +
					"\t\t\t\t<media value=\"ul[class=WB_media_a]\"></media>\n" +
					"\t\t\t\t<repeat value=\"div[class=func] li(1)\"></repeat>\n" +
					"\t\t\t\t<reply value=\"div[class=func] li(2)\"></reply>\n" +
					"\t\t\t</reserved>\n" +
					"\n" +
					"\t\t\t<author value=\"div[class=feed_content] a[class=name](nick-name)\"></author>\n" +
					"\t\t\t<links value=\"div[class=feed_content] a[class=name](href)\"></links>\n" +
					"\t\t\t<avatar value=\"div[class=avator] a img(src)\"></avatar>\n" +
					"\t\t\t<summary value=\"div[class=content] p[class=txt]\"></summary>\n" +
					"\t\t\t<href value=\"p[class=from] a(href)\"></href>\n" +
					"\t\t\t<pubdate value=\"p[class=from] a\"></pubdate>\n" +
					"\t\t\t<source value=\"div[class=from]  a[rel=nofollow]\"></source>\n" +
					"\t\t\t<media value=\"ul[class=WB_media_a]\"></media>\n" +
					"\t\t\t<repeat value=\"div[class=card-act] ul li(2)\"></repeat>\n" +
					"\t\t\t<reply value=\"div[class=card-act] ul li(3)\"></reply>\n" +
					"\t\t\t\n" +
					"\t\t</sina>\n" +
					"\t</weibo>\n" +
					"\t<tieba>\n" +
					"\t\t<source>baidu</source>\n" +
					"\t\t<!-- 百度贴吧 -->\n" +
					"\t\t<baidu\n" +
					"\t\t\turl=\"http://tieba.baidu.com/f/search/res?ie=utf-8&amp;qw=%word%&amp;rn=20\"\n" +
					"\t\t\tpage=\"pn=%page%\" sort=\"sm=2,sm=1\" encode=\"utf8\">\n" +
					"\t\t\t<result value=\"div[class=pager] span[class=s_nav_right]\"></result>\n" +
					"\t\t\t<all value=\"div[class=s_post_list]\"></all>\n" +
					"\t\t\t<items value=\"div[class=s_post]!div[class=post_user]\"></items>\n" +
					"\t\t\t<title value=\"span[class=p_title] a\"></title>\n" +
					"\t\t\t<info value=\"font[class=p_violet]\"></info>\n" +
					"\t\t\t<summary value=\"div[class=p_content]!a\"></summary>\n" +
					"\t\t\t<href value=\"span[class=p_title] a(href)\"></href>\n" +
					"\t\t\t<media value=\"div[class=p_media]\"></media>\n" +
					"\t\t\t<pubdate value=\"font[class=p_green]\"></pubdate>\n" +
					"\t\t\t<author value=\"font[class=p_violet](2)\"></author>\n" +
					"\t\t\t<source value=\"font[class=p_violet](1)\"></source>\n" +
					"\t\t</baidu>\n" +
					"\t</tieba>\n" +
					"\t\n" +
					"\t\n" +
					"\t<weixin>\n" +
					"\t\t<source>soso</source>\n" +
					"\t\t<!-- 微信 -->\n" +
					"\t\t\t<soso url=\"http://weixin.sogou.com/websearch/weixin/sogou.jsp?type=2&amp;query=%word%&amp;ie=utf8\" page=\"page=%page%\"\n" +
					"\t\t\tsort=\"type=2,type=2\" range=\"sourceid=inttime_day&amp;tsn=1,sourceid=inttime_week&amp;tsn=2,sourceid=inttime_month&amp;tsn=3,sourceid=inttime_year&amp;tsn=4\" encode=\"utf8\">\n" +
					"\t\t\t<result value=\"div[class=mun]\"></result>\n" +
					"\t\t\t<all value=\"ul[class=news-list]\"></all>\n" +
					"\t\t\t<items value=\"li\"></items>\n" +
					"\t\t\t<title value=\"h3 a\"></title>\n" +
					"\t\t\t<href value=\"h3 a(href)\"></href>\n" +
					"\t\t\t<img value=\"div[class=img-box] a img(src)\"></img>\n" +
					"\t\t\t<info value=\"div[class=s-p] a\"></info>\n" +
					"\t\t\t<author value=\"div[class=s-p] a\"></author>\n" +
					"\t\t\t\n" +
					"\t\t\t<pubdate value=\"div[class=s-p] span[class=s2](t)\"></pubdate>\n" +
					"\t\t\t<summary value=\"p[class=txt-info]\"></summary>\n" +
					"\t\t\n" +
					"\t\t</soso>\n" +
					"\t</weixin>\n" +
					"\t\n" +
					"\t\n" +
					"\t\n" +
					"\t\n" +
					"\t\n" +
					"\t<forum>\n" +
					"\t\t<source>tianya</source>\n" +
					"\t\t<!-- 天涯社区 -->\n" +
					"\t\t<tianya\n" +
					"\t\t\turl=\"http://search.tianya.cn/bbs?q=%word%\"\n" +
					"\t\t\tpage=\"pn=%page%\" range=\"f=0,f=0,f=0\" encode=\"utf8\" >\n" +
					"\t\t\t<result value=\"div[class=long-pages] em\"></result>\n" +
					"\t\t\t<all value=\"div[class=searchListOne] ul\"></all>\n" +
					"\t\t\t<items value=\"li\"></items>\n" +
					"\t\t\t<title value=\"div h3\"></title>\n" +
					"\t\t\t<summary value=\"div p\"></summary>\n" +
					"\t\t\t<href value=\"div h3 a(href)\"></href>\n" +
					"\t\t<!-- \t<media value=\"p[class=source] \"></media> -->\n" +
					"\t\t\t<links value=\"p[class=source] a(1)\"></links>\n" +
					"\t\t\t<pubdate value=\"p[class=source] span(1)\"></pubdate>\n" +
					"\t\t\t<info value=\"p[class=source] a(2)\"></info>\n" +
					"\t\t\t<source value=\"p[class=source] a(1)\"></source>\n" +
					"\t\t</tianya>\n" +
					"\t\t\n" +
					"\t\t\n" +
					"\t\t\n" +
					"\t\t\n" +
					"\t\t\n" +
					"\t</forum>\n" +
					"\t<wenda>\n" +
					"\t\t<source>baidu,sogou,qihu</source>\n" +
					"\t\t<!-- 百度知道 -->\n" +
					"\t\t<baidu\n" +
					"\t\t\turl=\"http://zhidao.baidu.com/search?word=%word%&amp;rn=10&amp;ie=utf8&amp;rn=10\"\n" +
					"\t\t\tpage=\"pn=(%page%-1)*10\" range=\"date=1,date=2,date=3\" encode=\"utf8\">\n" +
					"\t\t\t<result value=\"span[class=picker-header] span\"></result>\n" +
					"\t\t\t<all value=\"div[id=wgt-list]\"></all>\n" +
					"\t\t\t<items value=\"dl[class=dl]\"></items>\n" +
					"\t\t\t<title value=\"dt[class=mb-4] a\"></title>\n" +
					"\t\t\t<pubdate value=\"span[class=EC_zwd_date]\"></pubdate>\n" +
					"\t\t\t<summary value=\"dd[class=answer]!a[class=i-answer-text]\"></summary>\n" +
					"\t\t\t<href value=\"dt[class=mb-4] a(href)\"></href>\n" +
					"\t\t\t<info value=\"dd[class=explain]\"></info>\n" +
					"\t\t</baidu>\n" +
					"\t\t\t<!-- 搜搜问问 -->\n" +
					"\t\t<sogou url=\"http://wenwen.sogou.com/s/?w=%word%&amp;st=4&amp;ch=sp.pt\"\n" +
					"\t\t\tpage=\"pg=%page%\" range=\"sti=4,sti=2,sti=1\" encode=\"utf8\">\n" +
					"\t\t\t<result value=\"p[class=num-tips]\"></result>\n" +
					"\t\t\t<all value=\"div[id=main]\"></all>\n" +
					"\t\t\t<items value=\"div[class=vrwrap]\"></items>\n" +
					"\t\t\t<pubdate value=\"div[class=fb] cite\"></pubdate>\n" +
					"\t\t\t<info value=\"div[class=fb] cite\"></info>\n" +
					"\t\t\t<title value=\"h3 a\"></title>\n" +
					"\t\t\t<summary value=\"div[class=str-text-info] span\"></summary>\n" +
					"\t\t\t<href value=\"div[class=fb] a(href)\"></href>\n" +
					"\t\t\t<info value=\"div[class=fb] cite\"></info>\n" +
					"\t\t</sogou>\n" +
					"\t\t\t\t<!-- 360问答 -->\n" +
					"\t\t<qihu url=\"https://wenda.so.com/search/?q=%word%\"\n" +
					"\t\t\tpage=\"pn=%page%-1\" range=\",adv_t=w,adv_t=m\" encode=\"utf8\">\n" +
					"\t\t\t<result value=\"spanq[text=tab-resolved]\"></result>\n" +
					"\t\t\t<all value=\"div[id=qaresult] ul\"></all>\n" +
					"\t\t\t<items value=\"li\"></items>\n" +
					"\t\t\t<pubdate value=\"div[class=qa-i-ft]!div\"></pubdate>\n" +
					"\t\t\t<info value=\"div[class=qa-i-ft] span\"></info>\n" +
					"\t\t\t<title value=\"h3 a\"></title>\n" +
					"\t\t\t<summary value=\"div[class=qa-i-bd]\"></summary>\n" +
					"\t\t\t<href value=\"h3 a(href)\"></href>\n" +
					"\t\t\t<info value=\"div[class=qa-i-ft]\"></info>\n" +
					"\t\t</qihu>\n" +
					"\t</wenda>\n" +
					"\t\n" +
					"\t\n" +
					"\t\n" +
					"</root>";
			StringReader sr = new StringReader(xmlstr);
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse(new InputSource(sr));
		} catch (Exception e) {
			throw new Exception("元搜索配置文件丢失");
		}
		// 所有的类型节点，例：新闻、网页、微博等
		List<Node> types = new ArrayList<Node>();
		try {
			Node root = doc.getElementsByTagName("root").item(0);
			NodeList nodeList = root.getChildNodes();
			for (int i = 0; i < nodeList.getLength(); i++) {
				Node type = nodeList.item(i);
				if (type instanceof org.w3c.dom.Element && SearchTypeEnum.MS_ALL.getSearchType().indexOf(type.getNodeName()) != -1)
					types.add(type);
			}
		} catch (Exception e) {
			throw new Exception("元搜索配置文件格式异常");
		}
		for (Node type : types) {
			String typeName = type.getNodeName();
			Map<String, T> tMap = loadType(type);
			// 静态变量加载配置文件
			R.mspf.put(typeName, tMap);
		}
	}

	/**
	 * 加载类型节点
	 * 
	 * @param type
	 *            类型节点，如：新闻、网页、微博等
	 */

	private Map<String, T> loadType(Node type) {
		String typeName = type.getNodeName();
		NodeList categoryList = type.getChildNodes();
		String all = getNodeAll(typeName);
		// 百度、搜搜等节点
		List<Node> categories = new ArrayList<Node>();
		for (int i = 0; i < categoryList.getLength(); i++) {
			Node category = categoryList.item(i);
			if (category instanceof org.w3c.dom.Element
					&& all.indexOf(category.getNodeName()) != -1)
				categories.add(category);
		}
		Map<String, T> tMap = new HashMap<String, T>();
		// 来源节点，例：百度、搜搜等
		for (Node category : categories) {
			T t = new T();
			// 节点名称，例：baidu、soso
			String categoryName = category.getNodeName();
			t.name = categoryName;
			org.w3c.dom.Element e = (org.w3c.dom.Element) category;
			// 微博节点时需要加载账户
			if (e.hasAttribute("u") && e.hasAttribute("p")) {
				t.wb_u = e.getAttribute("u");
				t.wb_p = e.getAttribute("p");
				t.ip = e.getAttribute("ip");
				t.port = e.getAttribute("port");
				t.set = e.getAttribute("set");
				t.type = e.getAttribute("type");

				if (categoryName.equals("sina")) {

					t.authorization = "Basic "
							+ Base64.encodeBase64String((t.wb_u + ":" + t.wb_p).getBytes());

				}

			}

			if (e.hasAttribute("url"))
				t.url_base = e.getAttribute("url");
			if (e.hasAttribute("encode"))
				t.url_word_encode = e.getAttribute("encode");
			if (e.hasAttribute("page"))
				t.url_page = e.getAttribute("page");
			if (e.hasAttribute("sort"))
				t.url_sort = e.getAttribute("sort");
			if (e.hasAttribute("range"))
				t.url_range = e.getAttribute("range");
			if (e.hasAttribute("format"))
				t.url_date_format = e.getAttribute("format");
			if (e.hasAttribute("dup"))
				t.url_dup = e.getAttribute("dup");
			if (e.hasAttribute("mediatype"))
				t.url_mediatype = e.getAttribute("mediatype");
			// 子节点
			NodeList snList = category.getChildNodes();
			for (int i = 0; i < snList.getLength(); i++) {
				Node sn = snList.item(i);
				if (!(sn instanceof org.w3c.dom.Element))
					continue;
				e = (org.w3c.dom.Element) sn;
				String name = e.getNodeName();
				String value = e.getAttribute("value");
				if ("result".equals(name)) {
					t.rst_rst = value;
				} else if ("all".equals(name)) {
					t.rst_all = value;
				} else if ("items".equals(name)) {
					t.rst_list = value;
				} else if ("author".equals(name)) {
					t.rst_author = value;
				} else if ("links".equals(name)) {
					t.rst_links = value;
				} else if ("avatar".equals(name)) {
					t.rst_avatar = value;
				} else if ("summary".equals(name)) {
					t.rst_summary = value;
				} else if ("href".equals(name)) {
					t.rst_href = value;
				} else if ("pubdate".equals(name)) {
					t.rst_pubdate = value;
				} else if ("source".equals(name)) {
					t.rst_source = value;
				} else if ("media".equals(name)) {
					t.rst_media = value;
				} else if ("repeat".equals(name)) {
					t.rst_repeat = value;
				} else if ("reply".equals(name)) {
					t.rst_reply = value;
				} else if ("title".equals(name)) {
					t.rst_title = value;
				} else if ("img".equals(name)) {
					t.rst_img = value;
				} else if ("info".equals(name)) {
					t.rst_info = value;
				} else if ("more".equals(name)) {
					t.rst_more = value;
				} else if ("reserved".equals(name)) {
					// 转载节点
					t.rst_reserved = value;
					NodeList ssnList = e.getChildNodes();
					for (int j = 0; j < ssnList.getLength(); j++) {
						Node ssn = ssnList.item(j);
						if (!(ssn instanceof org.w3c.dom.Element))
							continue;
						e = (org.w3c.dom.Element) ssn;
						name = e.getNodeName();
						value = e.getAttribute("value");
						if ("summary".equals(name))
							t.rst_reserved_summary = value;
						else if ("pubdate".equals(name))
							t.rst_reserved_pubdate = value;
						else if ("href".equals(name))
							t.rst_reserved_href = value;
						else if ("source".equals(name))
							t.rst_reserved_source = value;
						else if ("media".equals(name))
							t.rst_reserved_media = value;
						else if ("repeat".equals(name))
							t.rst_reserved_repeat = value;
						else if ("reply".equals(name))
							t.rst_reserved_reply = value;
					}
				}
			}
			tMap.put(categoryName, t);
		}
		return tMap;
	}

	/**
	 * 获取分类下的所有节点
	 */
	private String getNodeAll(String typeName) {
		String all = "";
		if (SearchTypeEnum.MS_NEWS.getSearchType().equals(typeName))
			all = SearchTypeEnum.MS_NEWS_ALL.getSearchType();
		else if (SearchTypeEnum.MS_WEBPAGE.getSearchType().equals(typeName))
			all = SearchTypeEnum.MS_WEBPAGE_ALL.getSearchType();
		else if (SearchTypeEnum.MS_WEIBO.getSearchType().equals(typeName))
			all = SearchTypeEnum.MS_WEIBO_ALL.getSearchType();
		else if (SearchTypeEnum.MS_TIEBA.getSearchType().equals(typeName))
			all = SearchTypeEnum.MS_TIEBA_ALL.getSearchType();
		else if (SearchTypeEnum.MS_FORUM.getSearchType().equals(typeName))
			all = SearchTypeEnum.MS_FORUM_ALL.getSearchType();
		else if (SearchTypeEnum.MS_WENDA.getSearchType().equals(typeName))
			all = SearchTypeEnum.MS_WENDA_ALL.getSearchType();
		else if (SearchTypeEnum.MS_WEIXIN.getSearchType().equals(typeName))
			all = SearchTypeEnum.MS_WEIXIN_ALL.getSearchType();
		return all;
	}

}
